import 'dart:convert';

class ArticleItem {
  String title = "";

  String? cover;
  String link = "";
  List<String> tags = List.empty();

  ArticleItem({this.title = "", this.cover, this.link = "", this.tags = const []});

  String get id {
    final start = link.lastIndexOf("/");
    final end = link.lastIndexOf(".");
    if (start > -1 && end > -1) {
      return link.substring(start + 1, end);
    }
    return "";
  }

  @override
  String toString() {
    return jsonEncode(this);
  }

  ArticleItem.fromJson(Map<String, dynamic> json)
      : title = json['title'],
        link = json['link'],
        tags = (json['tags'] as List<dynamic>).map((e) => e.toString()).toList(),
        cover = json["cover"];

  Map<String, dynamic> toJson() {
    return {
      'title': title,
      'cover': cover,
      'link': link,
      'tags': tags,
    };
  }
}
